﻿
using System;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Windows.Forms;

namespace Mes_app
{
    public partial class Users_edit_Form : Form
    {
        int parent_id;
        private string[] array_cost_center = { "", "" };
        private string[] array_department = { "", "" };
        private string[] array_team = { "", "" };
        private string[] array_process = { "", "" };
        private string[] array_station = { "", "" };

        public Users_edit_Form(int id)
        {
            this.parent_id = id;
            InitializeComponent();
        }

        private void Btn_save_Click(object sender, EventArgs e)
        {
            int active = 1;
            if (active_1.Checked)
                active = 1;
            if (active_0.Checked)
                active = 0;

            string sqlValue = "UPDATE login_user SET login_user_id = @login_user_id,login_user_name = @login_user_name," +
               " login_user_password = @login_user_password,active = @active " +
               " WHERE id=@id ";
            SqlParameter[] parameters = {
             new SqlParameter("@id", parent_id),
             new SqlParameter("@login_user_id", Textadd_login_user_id.Text),
             new SqlParameter("@login_user_name", Textadd_login_user_name.Text),
             new SqlParameter("@login_user_password", Textadd_login_user_password.Text),
             new SqlParameter("@active", active),
                };

            int count_id = SqlHelper.ExecuteNonQuery(sqlValue, parameters);
            label_show.Text = count_id + "条记录,修改成功";
            Close();
        }

        private void Btn_cancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void Users_edit_Load(object sender, EventArgs e)
        {
            this.Width = 950;
            this.Height = 650;
            this.ComboBox_cost_center.DropDownHeight = 1;
            this.ComboBox_department.DropDownHeight = 1;
            this.ComboBox_team.DropDownHeight = 1;
            this.ComboBox_process.DropDownHeight = 1;
            ComboBox_station.Enabled = false;

            //赋值权限组选框
            GroupPowerService groupPowerService = new GroupPowerService();
            this.ComboBox_groupPower.DataSource = groupPowerService.GetList("");
            this.ComboBox_groupPower.DisplayMember = "group_power_name";
            this.ComboBox_groupPower.ValueMember = "group_power_no";
            this.ComboBox_groupPower.SelectedIndex = -1;
            this.tabControl1.SelectedIndex = 0;

            //赋值模块选框
            ModuleService moduleService = new ModuleService();
            this.ComboBox_module.DataSource = moduleService.GetList();
            this.ComboBox_module.DisplayMember = "module_name";
            this.ComboBox_module.ValueMember = "module_no";
            this.ComboBox_module.SelectedIndex = -1;
            this.ComboBox_module.AutoCompleteSource = AutoCompleteSource.ListItems;   //设置自动完成的源
            this.ComboBox_module.AutoCompleteMode = AutoCompleteMode.SuggestAppend;   //设置自动完成的的形式

            /*
            //赋值部门选框
            DepartmentService departmentService = new DepartmentService();
            this.ComboBox_department.DataSource = departmentService.GetList();
            this.ComboBox_department.DisplayMember = "department_name";
            this.ComboBox_department.ValueMember = "department_no";
            this.ComboBox_department.SelectedIndex = -1;
            */

            /*
            //赋值项目组选框
            TeamService teamService = new TeamService();
            this.ComboBox_team.DataSource = teamService.GetList();
            this.ComboBox_team.DisplayMember = "team_name";
            this.ComboBox_team.ValueMember = "team_no";
            this.ComboBox_team.SelectedIndex = -1;

            //赋值工序选框
            ProcessService processService = new ProcessService();
            this.ComboBox_process.DataSource = processService.GetAllList(parent_id);
            this.ComboBox_process.DisplayMember = "process_name";
            this.ComboBox_process.ValueMember = "process_no";
            this.ComboBox_process.SelectedIndex = -1;
            this.ComboBox_process.AutoCompleteSource = AutoCompleteSource.ListItems;   //设置自动完成的源
            this.ComboBox_process.AutoCompleteMode = AutoCompleteMode.SuggestAppend;   //设置自动完成的的形式
            */
            //begin 为DataGV增加复选框列
            DataGridViewCheckBoxColumn checkbox = new DataGridViewCheckBoxColumn();
            //列显示名称
            checkbox.HeaderText = "选择";
            checkbox.Name = "IsChecked";
            checkbox.TrueValue = true;
            checkbox.FalseValue = false;
            checkbox.DataPropertyName = "IsChecked";
            //列宽
            checkbox.Width = 50;
            //列大小不改变
            checkbox.Resizable = DataGridViewTriState.False;
            //添加的checkbox在dgv第一列
            this.DataGV.Columns.Insert(0, checkbox);
            //end 为DataGV增加复选框列

            ComboBox_groupPower.Enabled = false;
            btn_refresh.Enabled = false;
            btn_clear.Enabled = false;

            int active;
            //  tabControl1.SizeMode = TabSizeMode.Fixed;
            //  tabControl1.ItemSize = new Size(0, 1);
            // tabControl1.Appearance = FlatButtons;

            string sqlValue = "SELECT a.*  FROM login_user a WHERE  a.id=@id ";
            SqlParameter[] parameters = {
             new SqlParameter("@id", parent_id)
                };

            SqlDataReader dr = SqlHelper.ExecuteReader(sqlValue, parameters);
            while (dr.Read())
            {
                Textadd_login_user_id.Text = dr["login_user_id"].ToString().Trim();
                Textadd_login_user_name.Text = dr["login_user_name"].ToString().Trim();
                Textadd_login_user_password.Text = dr["login_user_password"].ToString().Trim();
                this.ComboBox_groupPower.Text = dr["group_power_name"].ToString().Trim();
                this.ComboBox_groupPower.SelectedValue = dr["group_power_no"].ToString().Trim();
                active = Convert.ToInt32(dr["active"].ToString());
                if (active == 1)
                {
                    active_1.Checked = true;
                    active_0.Checked = false;
                }
                else
                if (active == 0)
                {
                    active_1.Checked = false;
                    active_0.Checked = true;
                }
            }
            dr.Close();

            Query_module();
            if (DataGV_module.Rows.Count > 0)
            {
               // int rowindex = DataGV_module.CurrentRow.Index;
                String module_no = DataGV_module.Rows[0].Cells["module_no"].Value.ToString();
                String login_user_id = Textadd_login_user_id.Text;
                Query_power(login_user_id, module_no);

            }
            Query_team(parent_id);
            Query_process(parent_id);
            Query_department(parent_id);
            Query_cost_center(parent_id);
        }

        private void Btn_prev_Click(object sender, EventArgs e)
        {
            int count = tabControl1.TabCount;
            int i = tabControl1.SelectedIndex;
            if (i > 0)
            {
                tabControl1.SelectedIndex = i - 1;
            };

        }

        private void Btn_next_Click(object sender, EventArgs e)
        {
            int count = tabControl1.TabCount;
            int i = tabControl1.SelectedIndex;
            if (count > i)
            {
                tabControl1.SelectedIndex = i + 1;
            };
        }

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {

            if (this.tabControl1.SelectedIndex == 1)
            {
                //Query_power();
                ComboBox_groupPower.Enabled = true;
                btn_refresh.Enabled = true;
                btn_clear.Enabled = true;
            }
            else
            {
                ComboBox_groupPower.Enabled = false;
                btn_refresh.Enabled = false;
                btn_clear.Enabled = false;
            }
        }
 

        private void dataGV_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            int rowindex = DataGV.CurrentRow.Index;
            int id = Convert.ToInt32(DataGV.Rows[rowindex].Cells["id"].Value);

            string isadd = this.DataGV.Rows[rowindex].Cells["isadd"].Value.ToString();
            string isedit = this.DataGV.Rows[rowindex].Cells["isedit"].Value.ToString();
            string isdelete = this.DataGV.Rows[rowindex].Cells["isdelete"].Value.ToString();
            string isquery = this.DataGV.Rows[rowindex].Cells["isquery"].Value.ToString();
            string isprint = this.DataGV.Rows[rowindex].Cells["isprint"].Value.ToString();
            string ischeck = this.DataGV.Rows[rowindex].Cells["ischeck"].Value.ToString();
            string isbackcheck = this.DataGV.Rows[rowindex].Cells["isbackcheck"].Value.ToString();
            string isself = this.DataGV.Rows[rowindex].Cells["isself"].Value.ToString();
            // int.Parse(this.DataGV.Rows[rowindex].Cells["isadd"].Value.ToString());
            //sql 脚本
            // string sqlStr = string.Format("update UserInfo set {0} = {1} where userId = @userId", LieName, "@LieName");

            string sqlValue = " update login_user_power set isadd = @isadd,isedit = @isedit,isdelete = @isdelete,isquery = @isquery," +
                " isprint = @isprint,ischeck = @ischeck,isbackcheck = @isbackcheck,isself = @isself " +
                " where id = @id and parent_id = @parent_id ";
            SqlParameter[] parameters = {
                new SqlParameter("@id", id),
                new SqlParameter("@parent_id", parent_id),
                new SqlParameter("@isadd", isadd),
                new SqlParameter("@isedit", isedit),
                new SqlParameter("@isdelete", isdelete),
                new SqlParameter("@isquery", isquery),
                new SqlParameter("@isprint", isprint),
                new SqlParameter("@ischeck", ischeck),
                new SqlParameter("@isbackcheck", isbackcheck),
                new SqlParameter("@isself", isself),
                };
            int count_id = SqlHelper.ExecuteNonQuery(sqlValue, parameters);
            label_show.Text = count_id + "条记录,修改成功";
        }

        private void btn_refresh_Click(object sender, EventArgs e)
        {
            /*
            String str_groupPower = "";
            if (ComboBox_groupPower.Text.Equals(""))
            {
                MessageBox.Show("权限组不能为空！");
                return;
            }
            if (ComboBox_groupPower.Text != "")
                str_groupPower = ComboBox_groupPower.SelectedValue.ToString();
            */
            string sqlValue = "INSERT INTO login_user_power(parent_id,login_user_id,page_id,page_no,page_name,module_no,module_name)  " +
             " SELECT @parent_id,@login_user_id,a.id,a.page_no,a.page_name,a.module_no,b.page_name " +
             " FROM page_power a inner join page_power b on a.parent_id = b.id " + 
             " WHERE  1=1  " +
             " AND NOT EXISTS(SELECT 1 FROM login_user_power aa WHERE aa.page_id = a.id and aa.login_user_id = @login_user_id) ";
            /*
            string sqlValue = "INSERT INTO login_user_power(parent_id,login_user_id,group_power_no,group_power_name,page_no,page_name,module_no,module_name," +
             "       isadd,isedit,isdelete,isquery,isprint,ischeck,isbackcheck,isself)  " +
             " SELECT @parent_id,@login_user_id,a.group_power_no,a.group_power_name,b.page_no,b.page_name,b.module_no,b.module_name,   " +
             "        b.isadd,b.isedit,b.isdelete,b.isquery,b.isprint,b.ischeck,b.isbackcheck,b.isself  " +
             " FROM group_power a INNER JOIN group_power_page b ON a.id = b.parent_id " +
             " WHERE  a.group_power_no=@group_power_no  " +
             " AND NOT EXISTS(SELECT 1 FROM login_user_power aa WHERE aa.page_no = b.page_no and aa.login_user_id = @login_user_id) ";
            */
            SqlParameter[] parameters = {
                  new SqlParameter("@parent_id",parent_id),
                  new SqlParameter("@login_user_id",Textadd_login_user_id.Text),
                };
            SqlHelper.ExecuteNonQuery(sqlValue, parameters);

            //begin  更新模块
            string sqlValue2 = "update login_user_power set module_no = b.module_no " +
                " from login_user_power a,page_power b " +
                " where a.page_no = b.page_no and  a.login_user_id = @login_user_id " +
                " update  login_user_power set  module_name = b.module_name " +
                " from login_user_power a,v_module b " +
                " where a.module_no = b.module_no  and  a.login_user_id = @login_user_id ";
            SqlParameter[] parameters2 = {
                new SqlParameter("@login_user_id", Textadd_login_user_id.Text),
                };
            int count_id = SqlHelper.ExecuteNonQuery(sqlValue2, parameters2);
            //end 更新模块

            //begin  更新权限组名称
            /*
            string sqlValue2 = " update login_user set group_power_no = b.group_power_no, group_power_name = b.group_power_name  " +
            "from login_user a,login_user_power b  " +
            " where a.id = b.parent_id  and  b.parent_id = @parent_id ";
            SqlParameter[] parameters2 = {
                new SqlParameter("@parent_id", parent_id),
                };
            int count_id = SqlHelper.ExecuteNonQuery(sqlValue2, parameters2);
            */
            //end 更新权限组名称

            String login_user_id = Textadd_login_user_id.Text;
            int rowindex = DataGV_module.CurrentRow.Index;
            String module_no = DataGV_module.Rows[rowindex].Cells["module_no"].Value.ToString();
            Query_power(login_user_id, module_no);
        }

        private void btn_clear_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("是否清空已有权限？", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                //删除用户权限
                LoginUsersPowerService loginUsersPowerService = new LoginUsersPowerService();
                loginUsersPowerService.Delete(Textadd_login_user_id.Text);

                //begin  更新权限组名称
                string sqlValue2 = " update login_user set group_power_no = @group_power_no, group_power_name = @group_power_name  " +
                "from login_user a  where a.id = @parent_id ";
                SqlParameter[] parameters2 = {
                new SqlParameter("@parent_id", parent_id),
                new SqlParameter("@group_power_no", ""),
                new SqlParameter("@group_power_name", ""),
                };
                int count_id = SqlHelper.ExecuteNonQuery(sqlValue2, parameters2);
                //end 更新权限组名称

                String login_user_id = Textadd_login_user_id.Text;
                int rowindex = DataGV_module.CurrentRow.Index;
                String module_no = DataGV_module.Rows[rowindex].Cells["module_no"].Value.ToString();
                Query_power(login_user_id, module_no);
            }


        }

        private void Btn_all_Click(object sender, EventArgs e)
        {
            //遍历datagridview中的每一行，判断是否选中，若为选中，则选中
            for (int i = 0; i < DataGV.Rows.Count; i++)
            {
                if (Convert.ToBoolean(DataGV.Rows[i].Cells[0].Value) == false)
                {
                    DataGV.Rows[i].Cells[0].Value = true;
                }
                else
                {
                    continue;
                }
            }
        }

        private void Btn_cancel_all_Click(object sender, EventArgs e)
        {
            //遍历datagridview中的每一行，判断是否选中，若为选中，则选中
            for (int i = 0; i < DataGV.Rows.Count; i++)
            {
                if (Convert.ToBoolean(DataGV.Rows[i].Cells[0].Value) == true)
                {
                    DataGV.Rows[i].Cells[0].Value = false;
                }
                else
                {
                    continue;
                }
            }
        }

        private void Btn_add_delete_edit_query_Click(object sender, EventArgs e)
        {
            int str_id = 0;
            //遍历datagridview中的每一行，判断是否选中，若为选中，则选中
            for (int i = 0; i < DataGV.Rows.Count; i++)
            {
                if (Convert.ToBoolean(DataGV.Rows[i].Cells[0].Value) == true)
                {
                    str_id = Convert.ToInt32(DataGV.Rows[i].Cells["id"].Value);
                    string sqlValue = " update login_user_power set isadd = 1,isedit = 1,isdelete = 1,isquery = 1, isprint = 1 " +
                    " where id = @id  ";
                    SqlParameter[] parameters = {
                    new SqlParameter("@id", str_id),
                };
                    int count_id = SqlHelper.ExecuteNonQuery(sqlValue, parameters);
                    label_show.Text = count_id + "条记录,修改成功";
                }
                else
                {
                    continue;
                }
            }
            String login_user_id = Textadd_login_user_id.Text;
            int rowindex = DataGV_module.CurrentRow.Index;
            String module_no = DataGV_module.Rows[rowindex].Cells["module_no"].Value.ToString();
            Query_power(login_user_id, module_no);

        }

        private void Btn_cancel2_Click(object sender, EventArgs e)
        {
            int str_id = 0;
            //遍历datagridview中的每一行，判断是否选中，若为选中，则选中
            for (int i = 0; i < DataGV.Rows.Count; i++)
            {
                if (Convert.ToBoolean(DataGV.Rows[i].Cells[0].Value) == true)
                {
                    str_id = Convert.ToInt32(DataGV.Rows[i].Cells["id"].Value);
                    string sqlValue = " update login_user_power set isadd = 0,isedit = 0,isdelete = 0,isquery = 0, isprint = 0,ischeck = 0,isbackcheck = 0,isself = 0 " +
                    " where id = @id  ";
                    SqlParameter[] parameters = {
                    new SqlParameter("@id", str_id),
                };
                    int count_id = SqlHelper.ExecuteNonQuery(sqlValue, parameters);
                    label_show.Text = count_id + "条记录,修改成功";
                }
                else
                {
                    continue;
                }
            }
            // int rowindex = DataGV_module.CurrentRow.Index;
            // String module_no = DataGV_module.Rows[rowindex].Cells["module_no"].Value.ToString();
            // Query_power(module_no);

            String login_user_id = Textadd_login_user_id.Text;
            int rowindex = DataGV_module.CurrentRow.Index;
            String module_no = DataGV_module.Rows[rowindex].Cells["module_no"].Value.ToString();
            Query_power(login_user_id, module_no);


        }

        private void Btn_check_Click(object sender, EventArgs e)
        {
            int str_id = 0;
            //遍历datagridview中的每一行，判断是否选中，若为选中，则选中
            for (int i = 0; i < DataGV.Rows.Count; i++)
            {
                if (Convert.ToBoolean(DataGV.Rows[i].Cells[0].Value) == true)
                {
                    str_id = Convert.ToInt32(DataGV.Rows[i].Cells["id"].Value);
                    string sqlValue = " update login_user_power set ischeck = 1,isbackcheck = 1" +
                      " where id = @id  ";
                    SqlParameter[] parameters = {
                    new SqlParameter("@id", str_id),
                };
                    int count_id = SqlHelper.ExecuteNonQuery(sqlValue, parameters);
                    label_show.Text = count_id + "条记录,修改成功";
                }
                else
                {
                    continue;
                }
            }

            String login_user_id = Textadd_login_user_id.Text;
            int rowindex = DataGV_module.CurrentRow.Index;
            String module_no = DataGV_module.Rows[rowindex].Cells["module_no"].Value.ToString();
            Query_power(login_user_id, module_no);


        }

        private void Btn_cancel_check_Click(object sender, EventArgs e)
        {
            int str_id = 0;
            //遍历datagridview中的每一行，判断是否选中，若为选中，则选中
            for (int i = 0; i < DataGV.Rows.Count; i++)
            {
                if (Convert.ToBoolean(DataGV.Rows[i].Cells[0].Value) == true)
                {
                    str_id = Convert.ToInt32(DataGV.Rows[i].Cells["id"].Value);
                    string sqlValue = " update login_user_power set ischeck = 0,isbackcheck = 0" +
                      " where id = @id  ";
                    SqlParameter[] parameters = {
                    new SqlParameter("@id", str_id),
                };
                    int count_id = SqlHelper.ExecuteNonQuery(sqlValue, parameters);
                    label_show.Text = count_id + "条记录,修改成功";
                }
                else
                {
                    continue;
                }
            }

            int rowindex = DataGV_module.CurrentRow.Index;
            String login_user_id = Textadd_login_user_id.Text;
            String module_no = DataGV_module.Rows[rowindex].Cells["module_no"].Value.ToString();
            Query_power(login_user_id, module_no);

        }

        private void DataGV_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {

        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                Textadd_login_user_password.PasswordChar = '\0';   //显示输入
            }
            else
            {
                Textadd_login_user_password.PasswordChar = '*';   //显示*
            }
        }

        private void Button_team_add_Click(object sender, EventArgs e)
        {
            string sqlValue;
            if (ComboBox_team.Text.Equals(""))
            {
                MessageBox.Show("项目组不能为空！");
                return;
            }
            
            string str_team_no = array_team[0];
            string str_team_name = array_team[1];

           // string str_team_name = ComboBox_team.Text.ToString();
           // string str_team_no = ComboBox_team.SelectedValue.ToString();
            string str_login_user_id = Textadd_login_user_id.Text.ToString();

            sqlValue = "SELECT team_no  FROM login_user_team " +
                " WHERE parent_id = @parent_id " +
                " and team_no = @team_no ";
            SqlParameter[] parameters = {
             new SqlParameter("@team_no", str_team_no),
             new SqlParameter("@parent_id", parent_id),
                };
            String team_no = (String)SqlHelper.ExecuteScalar(sqlValue, parameters);
            if (str_team_no.Equals(team_no))
            {
                MessageBox.Show("项目组已重复！");
                return;
            }
            sqlValue = "INSERT INTO login_user_team(parent_id,login_user_id,team_no,team_name )  " +
                " VALUES(@parent_id,@login_user_id,@team_no,@team_name)";

            SqlParameter[] param_insert = {
                  new SqlParameter("@parent_id",parent_id),
                  new SqlParameter("@login_user_id",str_login_user_id),
                  new SqlParameter("@team_no",str_team_no),
                  new SqlParameter("@team_name",str_team_name),
                };
            int count_id = SqlHelper.ExecuteNonQuery(sqlValue, param_insert);
            label_show.Text = "项目组" + count_id + "添加成功";
            Query_team(parent_id);
        }
        private void Query_team(int parent_id)
        {
            string sqlValue = "SELECT a.id,a.team_no,a.team_name " +
                " FROM login_user_team a  " +
                " WHERE parent_id=@parent_id  ";
            SqlParameter[] parameters = {
             new SqlParameter("@parent_id", parent_id),
                };

            DataSet ds = SqlHelper.ExecuteDataSet(sqlValue, parameters);
            DataGV_team.DataSource = ds.Tables[0];
            DataGV_team.Columns[0].HeaderText = "ID";
            DataGV_team.Columns[1].HeaderText = "编号";
            DataGV_team.Columns[2].HeaderText = "名称";
            DataGV_team.Columns[0].Width = 70;
            DataGV_team.Columns[1].Width = 70;
            DataGV_team.Columns[2].Width = 80;
            DataGV_team.Columns[0].Visible = false;
            // DataGV2.Columns[3].Visible = false;
            //设置数据表格为只读
            // DataGV2.ReadOnly = true;
            //不允许添加行
            DataGV_team.AllowUserToAddRows = false;
            //背景为白色
            DataGV_team.BackgroundColor = Color.White;
            //只允许选中单行
            DataGV_team.MultiSelect = false;
            //整行选中
            DataGV_team.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

            if (DataGV_team.Rows.Count > 0)
                DataGV_team.CurrentCell = DataGV_team.Rows[0].Cells[1];
        }

        private void Button_team_delete_Click(object sender, EventArgs e)
        {
            if (DataGV_team.Rows.Count == 0)
                return;
            int rowindex = DataGV_team.CurrentRow.Index;
            int id = Convert.ToInt32(DataGV_team.Rows[rowindex].Cells[0].Value);
            if (MessageBox.Show("确认删除？", "是否删除？", MessageBoxButtons.OKCancel, MessageBoxIcon.Error) == DialogResult.OK)
            {
                string sqlValue = " DELETE FROM login_user_team WHERE id=@id ";
                SqlParameter[] parameters = {
                new SqlParameter("@id", id)
                };
                int count_id = SqlHelper.ExecuteNonQuery(sqlValue, parameters);
                label_show.Text = count_id + "条记录,删除成功";
                Query_team(parent_id);
            }
        }
        private void Query_process(int sid)
        {
            string sqlValue = "SELECT a.id,a.process_no,a.process_name " +
                " FROM login_user_process a  " +
                " WHERE parent_id=@parent_id  ";
            SqlParameter[] parameters = {
             new SqlParameter("@parent_id", sid),
                };

            DataSet ds = SqlHelper.ExecuteDataSet(sqlValue, parameters);
            DataGV_process.DataSource = ds.Tables[0];
            DataGV_process.Columns[0].HeaderText = "ID";
            DataGV_process.Columns[1].HeaderText = "编号";
            DataGV_process.Columns[2].HeaderText = "名称";
            DataGV_process.Columns[0].Width = 70;
            DataGV_process.Columns[1].Width = 70;
            DataGV_process.Columns[2].Width = 120;
            DataGV_process.Columns[0].Visible = false;
            // DataGV2.Columns[3].Visible = false;
            //设置数据表格为只读
            // DataGV2.ReadOnly = true;
            //不允许添加行
            DataGV_process.AllowUserToAddRows = false;
            //背景为白色
            DataGV_process.BackgroundColor = Color.White;
            //只允许选中单行
            DataGV_process.MultiSelect = false;
            //整行选中
            DataGV_process.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

            if (DataGV_process.Rows.Count > 0)
                DataGV_process.CurrentCell = DataGV_process.Rows[0].Cells[1];
        }


 

        private void Button_process_add_Click(object sender, EventArgs e)
        {
            string sqlValue;
            if (ComboBox_process.Text.Equals(""))
            {
                MessageBox.Show("项目组不能为空！");
                return;
            }
            string str_process_no = array_process[0];
            string str_process_name = array_process[1];

            // string str_process_name = ComboBox_process.Text.ToString();
            // string str_process_no = ComboBox_process.SelectedValue.ToString();
            string str_login_user_id = Textadd_login_user_id.Text.ToString();

            sqlValue = "SELECT process_no  FROM login_user_process " +
                " WHERE parent_id = @parent_id " +
                " and process_no = @process_no ";
            SqlParameter[] parameters = {
             new SqlParameter("@process_no", str_process_no),
             new SqlParameter("@parent_id", parent_id),
                };
            String process_no = (String)SqlHelper.ExecuteScalar(sqlValue, parameters);
            if (str_process_no.Equals(process_no))
            {
                MessageBox.Show("工序已重复！");
                return;
            }
            sqlValue = "INSERT INTO login_user_process(parent_id,login_user_id,process_no,process_name )  " +
                " VALUES(@parent_id,@login_user_id,@process_no,@process_name)";

            SqlParameter[] param_insert = {
                  new SqlParameter("@parent_id",parent_id),
                  new SqlParameter("@login_user_id",str_login_user_id),
                  new SqlParameter("@process_no",str_process_no),
                  new SqlParameter("@process_name",str_process_name),
                };
            int count_id = SqlHelper.ExecuteNonQuery(sqlValue, param_insert);
            label_show.Text = "工序组" + count_id + "添加成功";
            Query_process(parent_id);
        }

        private void Button_process_delete_Click(object sender, EventArgs e)
        {
            if (DataGV_process.Rows.Count == 0)
                return;
            int rowindex = DataGV_process.CurrentRow.Index;
            int id = Convert.ToInt32(DataGV_process.Rows[rowindex].Cells[0].Value);
            if (MessageBox.Show("确认删除？", "是否删除？", MessageBoxButtons.OKCancel, MessageBoxIcon.Error) == DialogResult.OK)
            {
                string sqlValue = " DELETE FROM login_user_process WHERE id=@id ";
                SqlParameter[] parameters = {
                new SqlParameter("@id", id)
                };
                int count_id = SqlHelper.ExecuteNonQuery(sqlValue, parameters);
                label_show.Text = count_id + "条记录,删除成功";
                Query_process(parent_id);
            }
        }

        private void Button_station_add_Click(object sender, EventArgs e)
        {
            string sqlValue;
            int process_rowindex = DataGV_process.CurrentRow.Index;
            int process_id = Convert.ToInt32(DataGV_process.Rows[process_rowindex].Cells[0].Value);
   
            if (ComboBox_station.Text.Equals(""))
            {
                MessageBox.Show("工站不能为空！");
                return;
            }
            string str_station_no = array_station[0];
            string str_station_name = array_station[1];

            // string str_station_no = ComboBox_station.SelectedValue.ToString();
            //  string str_station_name = ComboBox_station.Text.ToString();

            string str_login_user_id = Textadd_login_user_id.Text.ToString();

            sqlValue = "SELECT station_no  FROM login_user_station " +
                " WHERE parent_id = @parent_id " +
                " and station_no = @station_no ";
            SqlParameter[] parameters = {
             new SqlParameter("@station_no", str_station_no),
             new SqlParameter("@parent_id", process_id),
                };
            String station_no = (String)SqlHelper.ExecuteScalar(sqlValue, parameters);
            if (str_station_no.Equals(station_no))
            {
                MessageBox.Show("工站已重复！");
                return;
            }
            sqlValue = "INSERT INTO login_user_station(parent_id,login_user_id,station_no,station_name )  " +
                " VALUES(@parent_id,@login_user_id,@station_no,@station_name)";

            SqlParameter[] param_insert = {
                  new SqlParameter("@parent_id",process_id),
                  new SqlParameter("@login_user_id",str_login_user_id),
                  new SqlParameter("@station_no",str_station_no),
                  new SqlParameter("@station_name",str_station_name),
                };
            int count_id = SqlHelper.ExecuteNonQuery(sqlValue, param_insert);
            label_show.Text = "工站" + count_id + "添加成功";
            Query_station(process_id);
        }

        private void Query_station(int sid)
        {
            string sqlValue = "SELECT a.id,a.station_no,a.station_name " +
                " FROM login_user_station a  " +
                " WHERE parent_id=@parent_id  ";
            SqlParameter[] parameters = {
             new SqlParameter("@parent_id", sid),
                };

            DataSet ds = SqlHelper.ExecuteDataSet(sqlValue, parameters);
            DataGV_station.DataSource = ds.Tables[0];
            DataGV_station.Columns[0].HeaderText = "ID";
            DataGV_station.Columns[1].HeaderText = "编号";
            DataGV_station.Columns[2].HeaderText = "名称";
            DataGV_station.Columns[0].Width = 70;
            DataGV_station.Columns[1].Width = 70;
            DataGV_station.Columns[2].Width = 120;
            DataGV_station.Columns[0].Visible = false;
            // DataGV2.Columns[3].Visible = false;
            //设置数据表格为只读
            // DataGV2.ReadOnly = true;
            //不允许添加行
            DataGV_station.AllowUserToAddRows = false;
            //背景为白色
            DataGV_station.BackgroundColor = Color.White;
            //只允许选中单行
            DataGV_station.MultiSelect = false;
            //整行选中
            DataGV_station.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

            if (DataGV_station.Rows.Count > 0)
                DataGV_station.CurrentCell = DataGV_station.Rows[0].Cells[1];
        }

        private void Button_station_delete_Click(object sender, EventArgs e)
        {
            int process_rowindex = DataGV_process.CurrentRow.Index;
            int process_id = Convert.ToInt32(DataGV_process.Rows[process_rowindex].Cells[0].Value);

            if (DataGV_station.Rows.Count == 0)
                return;
            int rowindex = DataGV_station.CurrentRow.Index;
            int id = Convert.ToInt32(DataGV_station.Rows[rowindex].Cells[0].Value);
            if (MessageBox.Show("确认删除？", "是否删除？", MessageBoxButtons.OKCancel, MessageBoxIcon.Error) == DialogResult.OK)
            {
                string sqlValue = " DELETE FROM login_user_station WHERE id=@id ";
                SqlParameter[] parameters = {
                new SqlParameter("@id", id)
                };
                int count_id = SqlHelper.ExecuteNonQuery(sqlValue, parameters);
                label_show.Text = count_id + "条记录,删除成功";
                Query_station(process_id);
            }
        }

        private void DataGV_process_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            ComboBox_station.Enabled = true;

            int process_rowindex = DataGV_process.CurrentRow.Index;
            string process_no = DataGV_process.Rows[process_rowindex].Cells["process_no"].Value.ToString();
            int process_id = Convert.ToInt32(DataGV_process.Rows[process_rowindex].Cells[0].Value);
            Query_station(process_id);
            /*
            if (!process_no.Equals(""))
            {
                //赋值工站选框
                StationService stationService = new StationService();
                this.ComboBox_station.DataSource = stationService.GetListByProcess(process_no);
                this.ComboBox_station.DisplayMember = "station_name";
                this.ComboBox_station.ValueMember = "station_no";
                this.ComboBox_station.SelectedIndex = -1;
                this.ComboBox_station.AutoCompleteSource = AutoCompleteSource.ListItems;   //设置自动完成的源
                this.ComboBox_station.AutoCompleteMode = AutoCompleteMode.SuggestAppend;   //设置自动完成的的形式
            }
            */

        }

        private void DataGV_team_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            //自动编号，与数据无关
            Rectangle rectangle = new Rectangle(e.RowBounds.Location.X,
                e.RowBounds.Location.Y,
                DataGV_team.RowHeadersWidth - 4,
                e.RowBounds.Height);
            TextRenderer.DrawText(e.Graphics,
                (e.RowIndex + 1).ToString(),
                DataGV_team.RowHeadersDefaultCellStyle.Font,
                rectangle,
                DataGV_team.RowHeadersDefaultCellStyle.ForeColor,
                TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
        }

        private void DataGV_process_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            //自动编号，与数据无关
            Rectangle rectangle = new Rectangle(e.RowBounds.Location.X,
                e.RowBounds.Location.Y,
                DataGV_process.RowHeadersWidth - 4,
                e.RowBounds.Height);
            TextRenderer.DrawText(e.Graphics,
                (e.RowIndex + 1).ToString(),
                DataGV_process.RowHeadersDefaultCellStyle.Font,
                rectangle,
                DataGV_process.RowHeadersDefaultCellStyle.ForeColor,
                TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
        }

        private void DataGV_station_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            //自动编号，与数据无关
            Rectangle rectangle = new Rectangle(e.RowBounds.Location.X,
                e.RowBounds.Location.Y,
                DataGV_station.RowHeadersWidth - 4,
                e.RowBounds.Height);
            TextRenderer.DrawText(e.Graphics,
                (e.RowIndex + 1).ToString(),
                DataGV_station.RowHeadersDefaultCellStyle.Font,
                rectangle,
                DataGV_station.RowHeadersDefaultCellStyle.ForeColor,
                TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
        }

        private void DataGV_module_CellClick(object sender, DataGridViewCellEventArgs e)
        {
           int rowindex = DataGV_module.CurrentRow.Index;
            String login_user_id = Textadd_login_user_id.Text;
            String module_no = DataGV_module.Rows[rowindex].Cells["module_no"].Value.ToString();
            Query_power(login_user_id, module_no);
        }

        private void Btn_query2_Click(object sender, EventArgs e)
        {
            Query_module();
            int rowindex = DataGV_module.CurrentRow.Index;
            String login_user_id = Textadd_login_user_id.Text;
            String module_no = DataGV_module.Rows[rowindex].Cells["module_no"].Value.ToString();
            Query_power(login_user_id, module_no);
        }

        private void Query_module()
        {
            string sqlValue = "SELECT a.id,a.page_no as module_no,a.page_name as module_name  " +
                " FROM page_power a" +
                " WHERE 1=1 " +
                " AND parent_id = @parent_id " +
                " order by a.order_no ";
            SqlParameter[] parameters = {
                new SqlParameter("@parent_id", "0"),
                };

            DataSet ds = SqlHelper.ExecuteDataSet(sqlValue, parameters);
            if (ds.Tables.Count > 0)
            {
                DataGV_module.DataSource = ds.Tables[0];

                DataGV_module.Columns["id"].HeaderText = "ID";
                DataGV_module.Columns["module_no"].HeaderText = "模块编号";
                DataGV_module.Columns["module_name"].HeaderText = "模块";

                DataGV_module.Columns["id"].Width = 70;
                DataGV_module.Columns["module_no"].Width = 70;
                DataGV_module.Columns["module_name"].Width = 90;

                DataGV_module.Columns["id"].Visible = false;
                DataGV_module.Columns["module_no"].Visible = false;

                //不允许添加行
                DataGV_module.AllowUserToAddRows = false;
                //背景为白色
                DataGV_module.BackgroundColor = Color.White;
                //只允许选中单行
                DataGV_module.MultiSelect = false;
                //整行选中
                DataGV_module.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                // 列头隐藏
                // DataGV_module.ColumnHeadersVisible = false;
                // 行头隐藏
                // DataGV_module.RowHeadersVisible = false;

            }
        }
        private void Query_power(string login_user_id, string module_no)
        {
            if (module_no.Equals(""))
                return;

            string sqlValue = "SELECT a.id,a.module_name,a.page_no,a.page_name," +
                " a.isadd,a.isedit,a.isdelete,a.isquery,a.isprint,a.ischeck,a.isbackcheck,a.isself " +
                " FROM login_user_power a" +
                " WHERE 1=1" +
                // " and a.module_name <> '用户与权限'  " +
                " and  a.parent_id = @parent_id" +
                " and a.page_name like '%'+case when @page_name <> '' then @page_name else a.page_name end+'%' " +
                " and a.module_no = case when @module_no <> '' then @module_no else a.module_no end" +
                " and a.login_user_id =  @login_user_id  " +
                " order by a.page_no ";
            SqlParameter[] parameters = {
                new SqlParameter("@parent_id", parent_id),
                new SqlParameter("@page_name", TextBox_page_name.Text),
                new SqlParameter("@login_user_id", login_user_id),
                new SqlParameter("@module_no", module_no),
                };

            DataSet ds = SqlHelper.ExecuteDataSet(sqlValue, parameters);
            if (ds.Tables.Count > 0)
            {
                DataGV.DataSource = ds.Tables[0];

                DataGV.Columns["id"].HeaderText = "ID";
                DataGV.Columns["module_name"].HeaderText = "模块";
                DataGV.Columns["page_no"].HeaderText = "编号";
                DataGV.Columns["page_name"].HeaderText = "页面";
                DataGV.Columns["isadd"].HeaderText = "新增";
                DataGV.Columns["isedit"].HeaderText = "修改";
                DataGV.Columns["isdelete"].HeaderText = "删除";
                DataGV.Columns["isquery"].HeaderText = "查询";
                DataGV.Columns["isprint"].HeaderText = "打印";
                DataGV.Columns["ischeck"].HeaderText = "审核";
                DataGV.Columns["isbackcheck"].HeaderText = "弃审";
                DataGV.Columns["isself"].HeaderText = "仅操作自己";

                DataGV.Columns["id"].Width = 70;
                DataGV.Columns["module_name"].Width = 70;
                DataGV.Columns["page_no"].Width = 70;
                DataGV.Columns["page_name"].Width = 150;
                DataGV.Columns["isadd"].Width = 55;
                DataGV.Columns["isedit"].Width = 55;
                DataGV.Columns["isdelete"].Width = 55;
                DataGV.Columns["isquery"].Width = 55;
                DataGV.Columns["isprint"].Width = 55;
                DataGV.Columns["ischeck"].Width = 55;
                DataGV.Columns["isbackcheck"].Width = 55;
                DataGV.Columns["isself"].Width = 100;

                DataGV.Columns["id"].Visible = false;
                DataGV.Columns["isself"].Visible = false;
                DataGV.Columns["module_name"].Visible = false;
                DataGV.Columns["page_no"].Visible = false;
                //设置数据表格为只读
                // DataGV.ReadOnly = true;
                DataGV.Columns["module_name"].ReadOnly = true;
                DataGV.Columns["page_no"].ReadOnly = true;
                DataGV.Columns["page_name"].ReadOnly = true;

                DataGV.Columns["isadd"].ReadOnly = false;
                DataGV.Columns["isedit"].ReadOnly = false;
                DataGV.Columns["isdelete"].ReadOnly = false;
                DataGV.Columns["isquery"].ReadOnly = false;
                DataGV.Columns["isprint"].ReadOnly = false;
                DataGV.Columns["ischeck"].ReadOnly = false;
                DataGV.Columns["isbackcheck"].ReadOnly = false;
                DataGV.Columns["isself"].ReadOnly = false;
                //不允许添加行
                DataGV.AllowUserToAddRows = false;
                //背景为白色
                DataGV.BackgroundColor = Color.White;
                //只允许选中单行
                DataGV.MultiSelect = false;
                //整行选中
                DataGV.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

                ComboBox_groupPower.Enabled = !(DataGV.Rows.Count > 0);

            }
        }

        private void DataGV_module_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            //自动编号，与数据无关
            Rectangle rectangle = new Rectangle(e.RowBounds.Location.X,
                e.RowBounds.Location.Y,
                DataGV_module.RowHeadersWidth - 4,
                e.RowBounds.Height);
            TextRenderer.DrawText(e.Graphics,
                (e.RowIndex + 1).ToString(),
                DataGV_module.RowHeadersDefaultCellStyle.Font,
                rectangle,
                DataGV_module.RowHeadersDefaultCellStyle.ForeColor,
                TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
        }


        private void Query_department(int parent_id)
        {
            string sqlValue = "SELECT a.id,a.department_no,a.department_name " +
                " FROM login_user_department a  " +
                " WHERE parent_id=@parent_id  ";
            SqlParameter[] parameters = {
             new SqlParameter("@parent_id", parent_id),
                };

            DataSet ds = SqlHelper.ExecuteDataSet(sqlValue, parameters);
            DataGV_department.DataSource = ds.Tables[0];
            DataGV_department.Columns[0].HeaderText = "ID";
            DataGV_department.Columns[1].HeaderText = "编号";
            DataGV_department.Columns[2].HeaderText = "名称";
            DataGV_department.Columns[0].Width = 70;
            DataGV_department.Columns[1].Width = 70;
            DataGV_department.Columns[2].Width = 80;
            DataGV_department.Columns[0].Visible = false;
            // DataGV2.Columns[3].Visible = false;
            //设置数据表格为只读
            // DataGV2.ReadOnly = true;
            //不允许添加行
            DataGV_department.AllowUserToAddRows = false;
            //背景为白色
            DataGV_department.BackgroundColor = Color.White;
            //只允许选中单行
            DataGV_department.MultiSelect = false;
            //整行选中
            DataGV_department.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

            if (DataGV_department.Rows.Count > 0)
                DataGV_department.CurrentCell = DataGV_department.Rows[0].Cells[1];
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (DataGV_department.Rows.Count == 0)
                return;
            int rowindex = DataGV_department.CurrentRow.Index;
            int id = Convert.ToInt32(DataGV_department.Rows[rowindex].Cells[0].Value);
            if (MessageBox.Show("确认删除？", "是否删除？", MessageBoxButtons.OKCancel, MessageBoxIcon.Error) == DialogResult.OK)
            {
                string sqlValue = " DELETE FROM login_user_department WHERE id=@id ";
                SqlParameter[] parameters = {
                new SqlParameter("@id", id)
                };
                int count_id = SqlHelper.ExecuteNonQuery(sqlValue, parameters);
                label_show.Text = count_id + "条记录,删除成功";
                Query_department(parent_id);
            }
        }

        private void Button_dpt_add_Click(object sender, EventArgs e)
        {
            string sqlValue;
            if (ComboBox_department.Text.Equals(""))
            {
                MessageBox.Show("部门不能为空！");
                return;
            }

            string str_department_no = array_department[0];
            string str_department_name = array_department[1];

           // string str_department_name = ComboBox_department.Text.ToString();
           // string str_department_no = ComboBox_department.SelectedValue.ToString();
            string str_login_user_id = Textadd_login_user_id.Text.ToString();

            sqlValue = "SELECT department_no  " +
                " FROM login_user_department " +
                " WHERE parent_id = @parent_id " +
                " and department_no = @department_no ";
            SqlParameter[] parameters = {
             new SqlParameter("@department_no", str_department_no),
             new SqlParameter("@parent_id", parent_id),
                };
            String department_no = (String)SqlHelper.ExecuteScalar(sqlValue, parameters);
            if (str_department_no.Equals(department_no))
            {
                MessageBox.Show("部门已重复！");
                return;
            }
            sqlValue = "INSERT INTO login_user_department(parent_id,login_user_id,department_no,department_name )  " +
                " VALUES(@parent_id,@login_user_id,@department_no,@department_name)";

            SqlParameter[] param_insert = {
                  new SqlParameter("@parent_id",parent_id),
                  new SqlParameter("@login_user_id",str_login_user_id),
                  new SqlParameter("@department_no",str_department_no),
                  new SqlParameter("@department_name",str_department_name),
                };
            int count_id = SqlHelper.ExecuteNonQuery(sqlValue, param_insert);
            label_show.Text = "部门" + count_id + "添加成功";
            Query_department(parent_id);
        }

        private void Button_cost_center_add_Click(object sender, EventArgs e)
        {
            string sqlValue;
            if (ComboBox_cost_center.Text.Equals(""))
            {
                MessageBox.Show("成本中心不能为空！");
                return;
            }
            string str_cost_center_no = array_cost_center[0];
            string str_cost_center_name = array_cost_center[1];
            string str_login_user_id = Textadd_login_user_id.Text.ToString();

            sqlValue = "SELECT cost_center_no  " +
                " FROM login_user_cost_center " +
                " WHERE parent_id = @parent_id " +
                " and cost_center_no = @cost_center_no ";
            SqlParameter[] parameters = {
             new SqlParameter("@cost_center_no", str_cost_center_no),
             new SqlParameter("@parent_id", parent_id),
                };
            String cost_center_no = (String)SqlHelper.ExecuteScalar(sqlValue, parameters);
            if (str_cost_center_no.Equals(cost_center_no))
            {
                MessageBox.Show("成本中心已重复！");
                return;
            }
            sqlValue = "INSERT INTO login_user_cost_center(parent_id,login_user_id,cost_center_no,cost_center_name )  " +
                " VALUES(@parent_id,@login_user_id,@cost_center_no,@cost_center_name)";

            SqlParameter[] param_insert = {
                  new SqlParameter("@parent_id",parent_id),
                  new SqlParameter("@login_user_id",str_login_user_id),
                  new SqlParameter("@cost_center_no",str_cost_center_no),
                  new SqlParameter("@cost_center_name",str_cost_center_name),
                };
            int count_id = SqlHelper.ExecuteNonQuery(sqlValue, param_insert);
            label_show.Text = "部门" + count_id + "添加成功";
            Query_cost_center(parent_id);
        }

        private void Button_cost_center_delete_Click(object sender, EventArgs e)
        {
            if (DataGV_cost_center.Rows.Count == 0)
                return;
            int rowindex = DataGV_cost_center.CurrentRow.Index;
            int id = Convert.ToInt32(DataGV_cost_center.Rows[rowindex].Cells[0].Value);
            if (MessageBox.Show("确认删除？", "是否删除？", MessageBoxButtons.OKCancel, MessageBoxIcon.Error) == DialogResult.OK)
            {
                string sqlValue = " DELETE FROM login_user_cost_center WHERE id=@id ";
                SqlParameter[] parameters = {
                new SqlParameter("@id", id)
                };
                int count_id = SqlHelper.ExecuteNonQuery(sqlValue, parameters);
                label_show.Text = count_id + "条记录,删除成功";
                Query_cost_center(parent_id);
            }
        }

        private void Query_cost_center(int parent_id)
        {
            string sqlValue = "SELECT a.id,a.cost_center_no,a.cost_center_name " +
                " FROM login_user_cost_center a  " +
                " WHERE parent_id=@parent_id  ";
            SqlParameter[] parameters = {
             new SqlParameter("@parent_id", parent_id),
                };

            DataSet ds = SqlHelper.ExecuteDataSet(sqlValue, parameters);
            DataGV_cost_center.DataSource = ds.Tables[0];
            DataGV_cost_center.Columns[0].HeaderText = "ID";
            DataGV_cost_center.Columns[1].HeaderText = "编号";
            DataGV_cost_center.Columns[2].HeaderText = "名称";
            DataGV_cost_center.Columns[0].Width = 70;
            DataGV_cost_center.Columns[1].Width = 70;
            DataGV_cost_center.Columns[2].Width = 80;
            DataGV_cost_center.Columns[0].Visible = false;
            // DataGV2.Columns[3].Visible = false;
            //设置数据表格为只读
            // DataGV2.ReadOnly = true;
            //不允许添加行
            DataGV_cost_center.AllowUserToAddRows = false;
            //背景为白色
            DataGV_cost_center.BackgroundColor = Color.White;
            //只允许选中单行
            DataGV_cost_center.MultiSelect = false;
            //整行选中
            DataGV_cost_center.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

            if (DataGV_cost_center.Rows.Count > 0)
                DataGV_cost_center.CurrentCell = DataGV_cost_center.Rows[0].Cells[1];
        }

        private void ComboBox_cost_center_Click(object sender, EventArgs e)
        {
            SelectFromWhere_Form selectFromWhere_Form = new SelectFromWhere_Form("cost_center_no|cost_center_name||", "编号|名称||", "100|200||", "v_erp_cost_center",
                "1=1 AND effective ='Y' AND cost_center_no NOT IN (select cost_center_no from  login_user_cost_center where login_user_id = '"+ Textadd_login_user_id.Text.Trim()+ "') ");
            selectFromWhere_Form.ShowDialog();
            if (selectFromWhere_Form.DialogResult == DialogResult.OK)
            {
                array_cost_center[0] = selectFromWhere_Form.out_value;
                array_cost_center[1] = selectFromWhere_Form.out_value1;
                //  this.ComboBox_cost_center.SelectedValue = selectFromWhere_Form.out_value;
                this.ComboBox_cost_center.Text = selectFromWhere_Form.out_value1;
            }
        }

        private void ComboBox_department_Click(object sender, EventArgs e)
        {
            SelectFromWhere_Form selectFromWhere_Form = new SelectFromWhere_Form("department_no|department_name||", "编号|名称||", "100|200||", "department",
           "1=1  AND department_no NOT IN (select department_no from  login_user_department where login_user_id = '" + Textadd_login_user_id.Text.Trim() + "') ");
            selectFromWhere_Form.ShowDialog();
            if (selectFromWhere_Form.DialogResult == DialogResult.OK)
            {
                array_department[0] = selectFromWhere_Form.out_value;
                array_department[1] = selectFromWhere_Form.out_value1;
                //  this.ComboBox_cost_center.SelectedValue = selectFromWhere_Form.out_value;
                this.ComboBox_department.Text = selectFromWhere_Form.out_value1;
            }
        }

        private void ComboBox_team_Click(object sender, EventArgs e)
        {
            SelectFromWhere_Form selectFromWhere_Form = new SelectFromWhere_Form("team_no|team_name||", "编号|名称||", "100|200||", "team",
              "1=1  AND team_no NOT IN (select team_no from  login_user_team where login_user_id = '" + Textadd_login_user_id.Text.Trim() + "') ");
            selectFromWhere_Form.ShowDialog();
            if (selectFromWhere_Form.DialogResult == DialogResult.OK)
            {
                array_team[0] = selectFromWhere_Form.out_value;
                array_team[1] = selectFromWhere_Form.out_value1;
                //  this.ComboBox_cost_center.SelectedValue = selectFromWhere_Form.out_value;
                this.ComboBox_team.Text = selectFromWhere_Form.out_value1;
            }
        }

        private void ComboBox_process_Click(object sender, EventArgs e)
        {
            SelectFromWhere_Form selectFromWhere_Form = new SelectFromWhere_Form("process_no|process_name||", "编号|名称||", "100|200||", "process",
              "1=1  AND process_no NOT IN (select process_no from  login_user_process where login_user_id = '" + Textadd_login_user_id.Text.Trim() + "') ");
            selectFromWhere_Form.ShowDialog();
            if (selectFromWhere_Form.DialogResult == DialogResult.OK)
            {
                array_process[0] = selectFromWhere_Form.out_value;
                array_process[1] = selectFromWhere_Form.out_value1;
                //  this.ComboBox_cost_center.SelectedValue = selectFromWhere_Form.out_value;
                this.ComboBox_process.Text = selectFromWhere_Form.out_value1;
            }
        }

        private void ComboBox_station_Click(object sender, EventArgs e)
        {

            ComboBox_station.Enabled = true;

            int process_rowindex = DataGV_process.CurrentRow.Index;
            string process_no = DataGV_process.Rows[process_rowindex].Cells["process_no"].Value.ToString();
            int process_id = Convert.ToInt32(DataGV_process.Rows[process_rowindex].Cells[0].Value);
            Query_station(process_id);
            
            if (!process_no.Equals(""))
            {
                SelectFromWhere_Form selectFromWhere_Form = new SelectFromWhere_Form("station_no|station_name||", "编号|名称||", "100|200||",
                    "(select * from  station where process_no =  '" + process_no + "') a",
                  "1=1  AND station_no NOT IN (select station_no from  login_user_station  " +
                  " WHERE  login_user_id = '" + Textadd_login_user_id.Text.Trim() + "') ");
                selectFromWhere_Form.ShowDialog();
                if (selectFromWhere_Form.DialogResult == DialogResult.OK)
                {
                    array_station[0] = selectFromWhere_Form.out_value;
                    array_station[1] = selectFromWhere_Form.out_value1;
                    //  this.ComboBox_station.SelectedValue = selectFromWhere_Form.out_value;
                    this.ComboBox_station.Text = selectFromWhere_Form.out_value1;
                }
            }


        }
    }
}